{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "from nbfinder import NotebookFinder\n",
    "sys.meta_path.append(NotebookFinder())\n",
    "from util import get_timestamp\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from os.path import join\n",
    "import os\n",
    "from util import convert_bbox_minmax_to_cent_xywh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def grab_box_coords_for_timestep(fname, label_df, time_step):\n",
    "        ts = get_timestamp(fname)\n",
    "        \n",
    "        final_df=label_df.ix[ (label_df.month==ts.month) &\n",
    "                      (label_df.day==ts.day) & \n",
    "                      (label_df.year==ts.year) &\n",
    "                       (label_df.time_step == time_step)].copy()\n",
    "        final_df = final_df[ [\"xmin\", \"xmax\", \"ymin\", \"ymax\",\"category\"]]\n",
    "        return final_df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def make_labels_for_dataset(fname, labels_csv_file, time_steps_per_file=8):\n",
    "    '''takes in netcdf file and csv label file and outputs list of array of box coordinates for each time step '''\n",
    "\n",
    "    label_df = pd.read_csv(labels_csv_file)\n",
    "    box_list = []\n",
    "    for time_step in range(time_steps_per_file)[::2]:\n",
    "        box_df = grab_box_coords_for_timestep(fname,label_df, time_step)\n",
    "        boxes = np.asarray(box_df)\n",
    "        boxes = convert_bbox_minmax_to_cent_xywh(boxes)\n",
    "        box_list.append(boxes)\n",
    "    return box_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[452, 383,  58,  58,   2],\n",
       "        [174, 590,  80,  80,   3],\n",
       "        [142, 354,  80,  80,   3],\n",
       "        [122, 124,  80,  80,   3],\n",
       "        [693,  62,  80,  80,   3],\n",
       "        [543, 688, 149, 224,   4]]), array([[452, 381,  52,  52,   2],\n",
       "        [173, 604,  80,  80,   3],\n",
       "        [543, 688, 149, 224,   4]]), array([[452, 378,  48,  48,   2],\n",
       "        [543, 688, 149, 224,   4]]), array([[450, 375,  50,  50,   2],\n",
       "        [539, 492,  80,  80,   3],\n",
       "        [543, 688, 149, 224,   4]])]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "make_labels_for_dataset(\"/home/evan/data/climate/input/cam5_1_amip_run2.cam2.h2.1979-01-08-00000.nc\",\n",
    "                        \"/home/evan/data/climate/labels/labels.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
